package com.huawei.M01.iterator.A23;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 按字典排序在最后的子串
 *
 * @author zhiwu
 */
public class LastSubString {
    public String lastSubstring(String s) {
        Set<String> res = new HashSet<>();
        dfs(s, res);
        System.out.println(res);
        List<String> resultList = res.stream().sorted().collect(Collectors.toList());
        System.out.println(resultList);
        return resultList.get(res.size() - 1);
    }

    private void dfs(String s, Set<String> res) {
        if (s.length() == 1) {
            res.add(s);
            return;
        }
        for (int i = 0; i < s.length() - 1; i++) {
            for (int j = i; j < s.length(); j++) {
                res.add(s.substring(i, j + 1));
            }
        }
    }

    public static void main(String[] args) {
        System.out.println(new LastSubString().lastSubstring("leetcode"));
    }
}
